package com.nit.lab.service.Impl;

import com.nit.lab.dao.HisOrderDao;
import com.nit.lab.dao.OrderDao;
import com.nit.lab.dto.DtoOrder;
import com.nit.lab.dto.DtoOrderAllInfo;
import com.nit.lab.dto.DtoOrderFile;
import com.nit.lab.dto.DtohisOrder;
import com.nit.lab.dto.utils.DtoDataTable;
import com.nit.lab.service.OrderService;
import com.nit.lab.utils.SystemConstants;
import com.nit.lab.utils.UploadUtie;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderDao orderDao;

    @Resource
    private HisOrderDao hisOrderDao;

    @Override
    public Integer creatOrder(DtoOrderAllInfo dtoOrderAllInfo,Integer creat) throws Exception {
        Integer result = SystemConstants.Insert_Default;
        DtoOrder dtoOrder = new DtoOrder();
        DtoOrderFile dtoOrderFile = new DtoOrderFile();
        try {
            //order_file中的操作
            Double paperTypePrice = orderDao.getType(dtoOrderAllInfo.getPaperTypeId());
            Double paperColorPrice = orderDao.getColor(dtoOrderAllInfo.getPaperColorId());
            Double paperPrice = orderDao.getSize(dtoOrderAllInfo.getPaperSizeId());
            Double manny = dtoOrderAllInfo.getFilePrice()*paperTypePrice*paperColorPrice*paperPrice;
            dtoOrderFile.setFileId(dtoOrderAllInfo.getFileId());
            dtoOrderFile.setFilePrice(manny);
            dtoOrderFile.setPaperColorId(dtoOrderAllInfo.getPaperColorId());
            dtoOrderFile.setPaperSizeId(dtoOrderAllInfo.getPaperSizeId());
            dtoOrderFile.setPaperTypeId(dtoOrderAllInfo.getPaperTypeId());
            dtoOrderFile.setprintCount(dtoOrderAllInfo.getPrintCount());
            result = orderDao.creatOrder_file(dtoOrderFile.toBEAN());
            //order表中的操作
            dtoOrder.setOrderFiles(result + "");
            dtoOrder.setStoreId(dtoOrderAllInfo.getStoreId());
            System.out.println(dtoOrderAllInfo.getPrintCount());
            dtoOrder.setFileCount(dtoOrderAllInfo.getPrintCount());
            dtoOrder.setUserId(dtoOrderAllInfo.getUserId());
            dtoOrder.setOrderTime(UploadUtie.getDate());
            dtoOrder.setGetTime(dtoOrderAllInfo.getGetTime());
            orderDao.creatOrder(dtoOrder.ToBean(),creat);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public Integer getHisOrderDataTableCount(String search, Integer pre, Integer actorId) throws Exception {
        Integer result = 0;
        try {
            result = this.orderDao.getHisOrderDataTableCount(search, pre, actorId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public List<DtohisOrder> getHisOrderDataTable(DtoDataTable dtoDataTable, Integer pre, Integer actorId) throws Exception {
        List<DtohisOrder> result = new ArrayList<DtohisOrder>();
        result = orderDao.getHisOrderDataTable(dtoDataTable.getStart(), dtoDataTable.getLength(), dtoDataTable.getSearch(), pre, actorId);
        for (DtohisOrder dtohisOrder : result
                ) {
            String[] listOrderFile = dtohisOrder.getOrderFiles().split(",");
            List<DtoOrderFile> dtoOrderFiles =new ArrayList<DtoOrderFile>();
            for (int i = 0; i < listOrderFile.length; i++) {
                dtoOrderFiles.add(hisOrderDao.getFileInfoByOrderId(Integer.parseInt(listOrderFile[i])));
            }
            dtohisOrder.setDtoOrderFiles(dtoOrderFiles);
        }
        return result;
    }

    @Override
    public Integer chageOrderStatus(Integer orderId, Integer orderStatus) throws Exception {
        return orderDao.chageOrderStatus(orderId, orderStatus);
    }

}
